<template>
  <div class="container">
    <el-card>
      <div slot="header">
        <span style="font-weight: 700; font-size: 22px">文章列表</span>
      </div>
      <el-form>
        <el-row type="flex">
          <el-col :span="6">
            <el-form-item label="文章分类" label-width="70px">
              <el-select
                v-model="form.cate_id"
                placeholder="请选择"
                style="width: 260px"
              >
                <el-option
                  v-for="item in category"
                  :key="item.id"
                  :label="item.cate_name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="发布状态" label-width="60px">
              <el-select
                v-model="form.state"
                placeholder="请选择"
                style="width: 250px"
              >
                <el-option value="已发布">
                </el-option>
                <el-option value="草稿"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col>
            <el-button type="primary" @click="checked">筛选</el-button>
            <el-button type="info" plain>重置</el-button>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" @click="$router.push('add-article')">发表文章</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="list" border style="width: 100%">
        <el-table-column
          prop="title"
          label="文章标题"
          width="540"
        ></el-table-column>
        <el-table-column
          prop="cate_name"
          label="分类"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="pub_date"
          label="发表时间"
          width="360"
          :formatter="formDate"
        ></el-table-column>
        <el-table-column
          prop="state"
          label="状态"
          width="360"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button type="primary" size="small" @click="edit(row.id)">修改</el-button>
            <el-button type="danger" plain @click="delList(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[2, 3, 5, 10]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { delArticle, getArticleCategory, getArticleList } from '@/api/article'
export default {
  name: 'article-list',
  data () {
    return {
      category: [],
      form: {
        cate_id: '',
        state: ''
      },
      list: [],
      page: {
        pagenum: '1',
        pagesize: '2'
      },
      total: 0
    }
  },
  methods: {
    async edit (id) {
      this.$router.push(`/add-article?id=${id}`)
    },
    // 删除文章
    async delList (id) {
      await this.$confirm('确定要删除这篇文章吗')
      await delArticle(id)
      this.$message.success('删除成功')
      this.getArticleList()
    },
    // 筛选功能
    checked () {
      this.getArticleList()
      console.log(this.form)
    },
    async getArticleCategory () {
      const res = await getArticleCategory()
      this.category = res.data
      console.log(res.data)
    },
    async getArticleList () {
      const res = await getArticleList({ ...this.page, ...this.form })
      console.log(res)
      this.list = res.data
      this.total = res.total
    },
    handleSizeChange (val) {
      this.page.pagesize = val
      this.getArticleList()
    },
    handleCurrentChange (val) {
      this.page.pagenum = val
      this.getArticleList()
    },
    formDate (row, column, cellValue, index) {
      // 利用moment.js插件
      return this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  async created () {
    this.getArticleCategory()
    this.getArticleList()
  }
}
</script>

<style lang="less" scoped>
.el-col {
  margin-right: 25px;
}
</style>>
